//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "NothingExerciseValue.h"
using namespace Cephei::QL::Models::Marketmodels::Callability;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/Callability/MarketModelExerciseValue.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::CNothingExerciseValue (Cephei::Core::IVector<Double>^ rateTimes) : CMarketModelExerciseValue(CNothingExerciseValue::typeid)
{
    try
    {
#ifdef HANDLE
        _phNothingExerciseValue = NULL;
#endif
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        _ppNothingExerciseValue = new boost::shared_ptr<QuantLib::NothingExerciseValue> (new QuantLib::NothingExerciseValue ( _rateTimes ));
        SetMarketModelExerciseValue (boost::dynamic_pointer_cast<QuantLib::MarketModelExerciseValue> (*_ppNothingExerciseValue));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::CNothingExerciseValue (boost::shared_ptr<QuantLib::NothingExerciseValue>& childNative, Object^ owner) : CMarketModelExerciseValue(CNothingExerciseValue::typeid)
{
#ifdef HANDLE
	_phNothingExerciseValue = NULL;
#endif
	_ppNothingExerciseValue = &childNative;
    _ppMarketModelExerciseValue = new boost::shared_ptr<QuantLib::MarketModelExerciseValue> (boost::dynamic_pointer_cast<QuantLib::MarketModelExerciseValue> (*_ppNothingExerciseValue));
}
Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::CNothingExerciseValue (QuantLib::NothingExerciseValue& childNative, Object^ owner) : CMarketModelExerciseValue(CNothingExerciseValue::typeid)
{
#ifdef HANDLE
	_phNothingExerciseValue = NULL;
#endif
	_ppNothingExerciseValue = new boost::shared_ptr<QuantLib::NothingExerciseValue> (&childNative);
    _ppMarketModelExerciseValue = new boost::shared_ptr<QuantLib::MarketModelExerciseValue> (boost::dynamic_pointer_cast<QuantLib::MarketModelExerciseValue> (*_ppNothingExerciseValue));
    _NothingExerciseValueOwner = owner;
    _MarketModelExerciseValueOwner = owner;
}

Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::CNothingExerciseValue (CNothingExerciseValue^ copy) : CMarketModelExerciseValue(CNothingExerciseValue::typeid)
{
#ifdef HANDLE
	_phNothingExerciseValue = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppNothingExerciseValue = new boost::shared_ptr<QuantLib::NothingExerciseValue> (copy->GetShared());
        _ppMarketModelExerciseValue = new boost::shared_ptr<QuantLib::MarketModelExerciseValue> (boost::dynamic_pointer_cast<QuantLib::MarketModelExerciseValue> (*_ppNothingExerciseValue));
    }
}
Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::CNothingExerciseValue (PLATFORM::Type^ t) : CMarketModelExerciseValue(CNothingExerciseValue::typeid)
{
#ifdef HANDLE
	_phNothingExerciseValue = NULL;
#endif
	if (!t->IsSubclassOf(CNothingExerciseValue::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::CNothingExerciseValue (QuantLib::Handle<QuantLib::NothingExerciseValue>& childNative, Object^ owner)  : CMarketModelExerciseValue(CNothingExerciseValue::typeid)
{
	_phNothingExerciseValue = &childNative;
	_ppNothingExerciseValue = &static_cast<boost::shared_ptr<QuantLib::NothingExerciseValue>>(childNative.currentLink());
    _ppMarketModelExerciseValue = new boost::shared_ptr<QuantLib::MarketModelExerciseValue> (boost::dynamic_pointer_cast<QuantLib::MarketModelExerciseValue> (*_ppNothingExerciseValue));
    _NothingExerciseValueOwner = owner;
}
Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::CNothingExerciseValue (QuantLib::Handle<QuantLib::NothingExerciseValue> childNative)  : CMarketModelExerciseValue(CNothingExerciseValue::typeid)
{
	_phNothingExerciseValue = &childNative;
	_ppNothingExerciseValue = &static_cast<boost::shared_ptr<QuantLib::NothingExerciseValue>>(childNative.currentLink());
    _ppMarketModelExerciseValue = new boost::shared_ptr<QuantLib::MarketModelExerciseValue> (boost::dynamic_pointer_cast<QuantLib::MarketModelExerciseValue> (*_ppNothingExerciseValue));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::CNothingExerciseValue (QuantLib::NothingExerciseValue childNative)  : CMarketModelExerciseValue(CNothingExerciseValue::typeid)
{
#ifdef HANDLE
	_phNothingExerciseValue = NULL;
#endif
	_ppNothingExerciseValue = new boost::shared_ptr<QuantLib::NothingExerciseValue> (new QuantLib::NothingExerciseValue (childNative));
    _ppMarketModelExerciseValue = new boost::shared_ptr<QuantLib::MarketModelExerciseValue> (boost::dynamic_pointer_cast<QuantLib::MarketModelExerciseValue> (*_ppNothingExerciseValue));
}
#endif

Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::~CNothingExerciseValue ()
{
    if (_ppNothingExerciseValue != NULL)
    {
	    delete _ppNothingExerciseValue;
        _ppNothingExerciseValue = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::!CNothingExerciseValue ()
{
    if (_ppNothingExerciseValue != NULL)
    {
	    delete _ppNothingExerciseValue;
    }
}
QuantLib::NothingExerciseValue& Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::GetReference ()
{
    if (_ppNothingExerciseValue == NULL) throw REFNEW NativeNullException ();
	return **_ppNothingExerciseValue;
}
boost::shared_ptr<QuantLib::NothingExerciseValue>& Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::GetShared ()
{
    if (_ppNothingExerciseValue == NULL) throw REFNEW NativeNullException ();
	return *_ppNothingExerciseValue;
}
QuantLib::NothingExerciseValue* Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::GetPointer ()
{
    if (_ppNothingExerciseValue == NULL) throw REFNEW NativeNullException ();
	return &**_ppNothingExerciseValue;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::NothingExerciseValue>& Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::GetHandle ()
{
	if (_phNothingExerciseValue == NULL)
	{
		_phNothingExerciseValue = new Handle<QuantLib::NothingExerciseValue> (*_ppNothingExerciseValue);
	}
	return *_phNothingExerciseValue;
}
#endif
bool Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::HasNative () 
{
	return (_ppNothingExerciseValue != NULL);
}

Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppNothingExerciseValue)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::INothingExerciseValue^ Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::NextStep (Cephei::QL::Models::Marketmodels::ICurveState^ prm1)
{
    CCurveState^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CCurveState^> (prm1);
        _Cprm1->Lock();
        QuantLib::CurveState& _prm1 = static_cast<QuantLib::CurveState&> (_Cprm1->GetReference ()); 
    	(*_ppNothingExerciseValue)->nextStep ( _prm1 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::NumberOfExercises::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppNothingExerciseValue)->numberOfExercises ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppNothingExerciseValue)->possibleCashFlowTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::INothingExerciseValue^ Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppNothingExerciseValue)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Callability::INothingExerciseValue^ Cephei::QL::Models::Marketmodels::Callability::CNothingExerciseValue_Factory::Create (Cephei::Core::IVector<Double>^ rateTimes)
{
    return REFNEW CNothingExerciseValue ( rateTimes);
}
